﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace DataShow
{
    /// <summary>
    /// CommonHandler 的摘要说明
    /// </summary>
    public class CommonHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.QueryString["action"];
            switch (action)
            {
                case "get_xunyue": context.Response.Write(GetXunyue(context)); break;
                case "get_name": context.Response.Write(GetName(context)); break;
            }
        }

        class NameUserPair
        {
            public string Name { get; set; }
            public string UserName { get; set; }
        }
        private string GetName(HttpContext context)
        {
            
            string sql = "select username,name from account_forecaster";
            DataTable dt = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text);
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)//每一行信息，新建一个Dictionary<string,object>,将该行的每列信息加入到字典
                {

                    Dictionary<string, object> result = new Dictionary<string, object>();

                    foreach (DataColumn dc in dt.Columns)
                    {

                        result.Add(dc.ColumnName, dr[dc].ToString());

                    }
                    list.Add(result);
                }
                System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer()
                ;
                StringBuilder sb = new StringBuilder();
                ser.Serialize(list, sb);
                return sb.ToString();
            }
            else
            {
                return "";
            }
        }

        private string GetXunyue(HttpContext context)
        {
            string id = context.Request.QueryString["id"];
            string sql = "select * from xunyue where id=" + id;
            try
            {
                DataTable dt = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text);
                DataRow row = dt.Rows[0];
                return row["id"].ToString() + "," + row["month"].ToString() + "," + row["xun"].ToString() + "," + row["content"].ToString();
            }
            catch
            {
                return "error";
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}